package com.example.shaidemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UploadHotelActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "UploadHotelActivity";

    EditText city_input,name_input,time_input,price_input,note_input;
    Spinner spinner,spinner2;
    RadioGroup radioGroup;
    String holiday_text,type_text,city_txt,name_text,time_txt,room_text,price_text,note_text;
    String []hotelArray;
    String []typeArray;
    ImageView photo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_hotel);

        city_input = findViewById(R.id.h_city_input);
        name_input = findViewById(R.id.h_name_input);
        time_input =  findViewById(R.id.h_time_input);
        price_input = findViewById(R.id.h_price_input);
        note_input = findViewById(R.id.h_note_input);
        //设置单选框，通过设置监听事件并获取值
        radioGroup = findViewById(R.id.holiday_input);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                holiday_text = checkId == R.id.yes_input? "是" : "否" ;
                Log.i(TAG, "是否为节假日: "  + holiday_text);
            }
        });
        // 设置下拉列表，通过事件监听获取值
        spinner = findViewById(R.id.h_type_spinner);
        hotelArray = getResources().getStringArray(R.array.hotelArray);           //获取下拉列表的所有值
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                // 获取被选中的值
                type_text = hotelArray[position];
                Log.i(TAG, "被选择的酒店类型为: " + type_text);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {         }
        });
        // 设置房间类型
        spinner2 = findViewById(R.id.h_room_spinner);
        typeArray = getResources().getStringArray(R.array.HotelType);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                room_text = typeArray[position];
                Log.i(TAG, "被选择的房间类型为: " + room_text);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) { }
        });

        // 返回原页面
        ImageView pic = findViewById(R.id.collection_pic);
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UploadHotelActivity.this,MainActivity.class);
                intent.putExtra("fragment_flag",2);
                startActivity(intent);
            }
        });

        //提交照片
        photo = findViewById(R.id.h_open_camera);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //执行OpenCamera（）方法
                OpenCamera();
            }
        });
    }

    @Override
    public void onClick(View view) {
        // 获取用户输入内容
        city_txt = city_input.getText().toString().trim();
        time_txt = time_input.getText().toString().trim();
        name_text = name_input.getText().toString().trim();
        price_text = price_input.getText().toString().trim();
        note_text = note_input.getText().toString().trim();

        // 判断输入是否为空、是否合法
        CheckInput ck = new CheckInput();
        boolean flag = ck.CheckInputNull(city_txt) && ck.CheckInputNull(time_txt) && ck.CheckInputNull(name_text) && ck.CheckInputNull(room_text);
        boolean flag1 = ck.CheckInputNull(price_text) &&ck.CheckInputNull(note_text) ;
        if(flag && flag1){
            Log.i(TAG, "onClick: 全部输入不为空！");
            // 检查数字型数据是否合法
//            boolean f = ck.CheckNumber(time_txt) && ck.CheckNumber(price_text) ;
//            if(f){
                //获取上传时间
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
                String time =  sdf.format(date);
                // 将数据存储到数据库
                SharedPreferences sharedPreferences = getSharedPreferences("newHotel", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("class","酒店");
                editor.putString("status","未审核");
                editor.putString("time",time);
                editor.putString("city",city_txt);
                String str1 = type_text+"    "+holiday_text;
                editor.putString("mes",str1);
                String str2 = "￥ "+price_text;
                editor.putString("price",str2);
                editor.commit();
                Log.i(TAG, "onActivityResult: 数据已保存到sharedPreferences");


                //提示消息
                Toast.makeText(this,"上传成功！",Toast.LENGTH_SHORT).show();

                // 返回页面
                finish();
//            }else{
//                //存在非数字
//                Toast.makeText(this,"请输入规范的数据！！",Toast.LENGTH_SHORT).show();
//            }
        }else{
            // 提示信息
            Toast.makeText(this,"输入不能为空，请再次检查输入！",Toast.LENGTH_SHORT).show();
        }

    }

    //打开拍照界面
    public void OpenCamera(){
        Intent open = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(open,2);
    }

    // 获取相机所拍到的图片，并执行SaveScreenShot()方法保存图片
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 2 && resultCode == RESULT_OK && null !=data) {
            Bundle bundle = data.getExtras();
            //获取相机返回的数据，并转换为Bitmap图片格式，即缩略图
            Bitmap bitmap = (Bitmap) bundle.get("data");
            //保存图片
            SavePicture sa = new SavePicture();
            String path = sa.SaveScreenShot(bitmap, this);
            Log.i(TAG, "onActivityResult: Uri ===" + path);
            //显示所拍摄的照片
            photo.setImageBitmap(bitmap);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}